package com.max.sportmanage.config.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.max.sportmanage.common.Result;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @program: java-sport-manage
 * @description:无权限时返回结果
 * @author: max-qaq
 * @create: 2021-10-23 23:09
 **/

@Component
public class JwtAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest Request, HttpServletResponse Response, AccessDeniedException e) throws IOException, ServletException {
        Response.setStatus(403);
        Response.setCharacterEncoding("UTF-8");
        Response.setContentType("application/json");
        PrintWriter writer = Response.getWriter();
        writer.write(new ObjectMapper().writeValueAsString(Result.FAIL("权限不足！")));
        writer.flush();
        writer.close();
    }
}
